package controller

import (
	"net/http"
	"poker/comm"
	"poker/env"
	"poker/model"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/smallnest/rpcx/log"
)

func GetUserInfo(c *gin.Context) {
	req := &struct {
		Mode                int32             `json:"mode" form:"mode" binding:"required"`
		Device              string            `json:"device" form:"device"`
		DeviceID            string            `json:"device_id" form:"device_id" binding:"required"`
		Mobile              string            `json:"mobile" form:"mobile"`
		Code                string            `json:"code" form:"code"`
		WechatCode          string            `json:"wechat_code" form:"wechat_code"`
		WechatInfo          *model.WechatInfo `json:"wechat_info" form:"wechat_info"`
		OneClickToken       string            `json:"one_click_token" form:"one_click_token"`
		OneClickAccessToken string            `json:"one_click_access_token" form:"one_click_access_token"`
		RobotCode           string            `json:"robot_code" form:"robot_code"`
		IsCheck             bool              `json:"is_check" form:"is_check"` //是否审核
		Channel             int32             `json:"channel" form:"channel"`   // 登录渠道 ios,android,miniWechat
		//三方登录参数
		GameId       int32  `json:"game_id" form:"game_id"`
		ChannelId    int32  `json:"channel_id" form:"channel_id"`
		ChannelToken string `json:"token" form:"token"`
	}{}

	if err := c.ShouldBind(req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  err.Error(),
		})
		return
	}
	log.Info("Login req", req)

	var err error

	// var user *model.User

	// var unionid string

	switch req.Mode {
	case comm.Login_Guest:

	case comm.Login_Mobile:

	case comm.Login_WechatNative:

	case comm.Login_WechatMini:

	case comm.Login_OneClick:

	case comm.Login_Coin_Channel_FP:

	case comm.Login_Coin_Channel_ZHQP:

	default:
		log.Errorf("login failed, err: %v", err)
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "不支持的登录方式",
		})
		return
	}

	var res = make(map[string]interface{})
	addr := "ws://" + env.GlobalData.Server.Host + ":" + strconv.Itoa(env.GlobalData.Server.Port)
	res["token"] = req.ChannelToken
	res["server"] = gin.H{"addr": addr, "version": 20251009}

	log.Infof("login success,mobile %s, code:%s", req.Mobile, req.Code)
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": res,
	})
}

// 检测token 是否有效
func CheckTokenIsValid(c *gin.Context) {
	req := &struct {
		Token string `json:"token" form:"token" binding:"required"`
	}{}
	if err := c.ShouldBind(req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  err.Error(),
		})
		return
	}

	// 登录服务
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": "",
	})
}
